package com.itlnn.xfhttp.comment;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.itlnn.xfhttp.able.BaseRequest;

import java.util.List;
import java.util.Map;

/**
 * 网络请求的工具类
 * @User :      xuyuqiang
 * @Date :      2016/12/27 下午2:19
 * @Version :   1.0.0
 * @ClassName : HttpUtils
 **/
public class HttpUtils {

    /**
     * 参数的封装方法：会把参数封装成一个json对象
     * @param params    参数map
     * @return
     *      json对象字符串
     */
    public static String initJsonUrl(Map<String , Object> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (String key : params.keySet()) {
            sb.append("\"" + key + "\":");
            Object value = params.get(key) ;
            if (value instanceof BaseRequest || value instanceof List){
                sb.append(new Gson().toJson(value)) ;
            }else if(value instanceof String) {
                sb.append("\"" + (String)value + "\"") ;
            }else if(value == null){
                sb.append(value) ;
            }else{
                sb.append(String.valueOf(value)) ;
            }
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        if (TextUtils.isEmpty(sb)) {
            return "";
        }
        return sb.toString();
    }
}
